﻿Imports QLTHUVIEN.DTO
Imports QLTHUVIEN.BUS
Public Class frmQuanLySach

    Private Sub layDanhSachSach()
        Dim sachBus As New SachBUS()
        Dim dt As DataTable = sachBus.layDanhSachSach()
        dtgDanhSachSach.DataSource = dt
    End Sub

    Private Sub loadTheLoai()
        Dim tlSachBus As New TheLoaiSachBUS()
        Dim dt As DataTable = tlSachBus.layDanhSach()
        cboTheLoai.DataSource = dt
        cboTheLoai.ValueMember = "MaTheLoai"
        cboTheLoai.DisplayMember = "TenTheLoai"
    End Sub

    Private Sub loadNhaXuatBan()
        Dim nxbBus As New NhaXuatBanBUS()
        Dim dt As DataTable = nxbBus.layDanhSach()
        cboNXB.DataSource = dt
        cboNXB.ValueMember = "MaNhaXuatBan"
        cboNXB.DisplayMember = "TenNhaXuatBan"
    End Sub

    Private Sub loadNguoiNhap()
        Dim nvienBus As New NhanVienBUS()
        Dim dt As DataTable = nvienBus.layNhanVienBoPhan("Thủ Kho")
        cboNguoiNhap.DataSource = dt
        cboNguoiNhap.ValueMember = "MaNhanVien"
        cboNguoiNhap.DisplayMember = "TenNhanVien"

    End Sub


    Private Sub frmQuanLySach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        layDanhSachSach()
        loadTheLoai()
        loadNhaXuatBan()
        loadNguoiNhap()

    End Sub

    Private Sub ganGiaTri()
        txtMaSach.Text = dtgDanhSachSach.CurrentRow.Cells(0).Value.ToString()
        txtTenSach.Text = dtgDanhSachSach.CurrentRow.Cells(1).Value.ToString()
        txtTacGia.Text = dtgDanhSachSach.CurrentRow.Cells(2).Value.ToString()
        txtNamXuatBan.Text = dtgDanhSachSach.CurrentRow.Cells(3).Value.ToString()
        dtpNgayNhap.Text = dtgDanhSachSach.CurrentRow.Cells(4).Value.ToString()
        txtTriGia.Text = dtgDanhSachSach.CurrentRow.Cells(5).Value.ToString()

        cboTheLoai.Text = dtgDanhSachSach.CurrentRow.Cells(6).Value.ToString()
        cboNXB.Text = dtgDanhSachSach.CurrentRow.Cells(7).Value.ToString()
        cboNguoiNhap.Text = dtgDanhSachSach.CurrentRow.Cells(8).Value.ToString()

    End Sub

    Private Sub dtgDanhSachSach_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgDanhSachSach.CellClick
        ganGiaTri()
    End Sub

    Private Function laySach(ByRef sachDto As SachDTO) As Boolean
        Try
            sachDto.TenSach = txtTenSach.Text
            sachDto.TacGia = txtTacGia.Text
            sachDto.NamXuatBan = Integer.Parse(txtNamXuatBan.Text)
            sachDto.NgayNhap = Date.Parse(dtpNgayNhap.Text)
            sachDto.TriGia = Double.Parse(txtTriGia.Text)
            sachDto.MaTheLoai = Integer.Parse(cboTheLoai.SelectedValue)
            sachDto.MaNhaXuatBan = Integer.Parse(cboNXB.SelectedValue)
            sachDto.MaNhanVien = Integer.Parse(cboNguoiNhap.SelectedValue)
            sachDto.IsDelete = False
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function ktNamXuatBan(ByVal namXuatBan As Integer)
        Dim _gioiHanNamXuatBan As Integer
        Dim tsoBus As New ThamSoBUS()
        Dim dt As DataTable = tsoBus.layDanhSach()
        For Each row In dt.Rows
            _gioiHanNamXuatBan = row("GioiHanNamXuatBan")
        Next

        If (Now.Date.Year() - namXuatBan) > _gioiHanNamXuatBan Or namXuatBan > Now.Date.Year Then
            Return False
        End If
        Return True
    End Function

    Private Sub btnThemSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemSach.Click
        Dim sachDto As New SachDTO()
        If laySach(sachDto) = False Then
            MessageBox.Show("Kiem tra lai du lieu")
            Return
        End If

        If ktNamXuatBan(sachDto.NamXuatBan) = False Then
            MessageBox.Show("Kiem tra nam xuat ban")
            Return
        End If

        Dim sachBus As New SachBUS()
        sachBus.themSach(sachDto)

        layDanhSachSach()
    End Sub

    Private Sub btnSuaSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuaSach.Click
        If txtMaSach.Text = "" Then
            Return
        End If
        Dim sachDto As New SachDTO()
        sachDto.MaSach = Integer.Parse(txtMaSach.Text)

        If laySach(sachDto) = False Then
            MessageBox.Show("Kiem tra lai du lieu")
            Return
        End If

        If ktNamXuatBan(sachDto.NamXuatBan) = False Then
            MessageBox.Show("Kiem tra nam xuat ban")
            Return
        End If

        Dim sachBus As New SachBUS()
        sachBus.suaSach(sachDto)

        layDanhSachSach()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Dim kq = MessageBox.Show("Ban co muon thoat khong", "QUAN LY THU VIEN", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If kq = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub btnXoaSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaSach.Click
        Dim kq = MessageBox.Show("Ban co muon Thoat hay khong", "QLTHUVIEN", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If kq = Windows.Forms.DialogResult.No Then
            Return
        End If

        If txtMaSach.Text = "" Then
            Return
        End If
        Dim sachDto As New SachDTO()
        sachDto.MaSach = Integer.Parse(txtMaSach.Text)
        sachDto.IsDelete = True

        Dim sachBus As New SachBUS()
        sachBus.xoaSach(sachDto)

        layDanhSachSach()
    End Sub
End Class